package com.kongjs.application.controller.rest;

import com.kongjs.application.convert.LoginLogConverter;
import com.kongjs.application.model.dto.PageDTO;
import com.kongjs.application.model.dto.PageQueryDTO;
import com.kongjs.application.model.dto.QueryLoginLogDTO;
import com.kongjs.application.model.dto.QueryLoginParamDTO;
import com.kongjs.application.model.vo.PageQueryVO;
import com.kongjs.application.model.vo.PageVO;
import com.kongjs.application.model.vo.QueryLoginLogVO;
import com.kongjs.application.model.vo.QueryLoginParamVO;
import com.kongjs.application.service.ILoginLogService;
import com.kongjs.application.util.PageUtil;
import com.kongjs.common.rest.annotation.Rest;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Rest
@RestController
@RequestMapping("/login/log")
public class LoginLogController {
    @Resource
    private LoginLogConverter loginLogConverter;
    @Resource
    private ILoginLogService loginLogService;

    @PreAuthorize("hasAuthority('api:loginLog:remove:one')")
    @DeleteMapping("/{id}")
    public boolean remove(@PathVariable("id") Long id) {
        return loginLogService.removeById(id);
    }

    @PreAuthorize("hasAuthority('api:loginLog:remove:list')")
    @DeleteMapping
    public boolean remove(@RequestBody List<Long> ids) {
        return loginLogService.removeByIds(ids);
    }

    @PreAuthorize("hasAuthority('api:loginLog:query:page')")
    @GetMapping("/page")
    public PageVO<QueryLoginLogVO> page(@Validated PageQueryVO vo, QueryLoginParamVO paramVO) {
        PageQueryDTO dto = new PageQueryDTO();
        dto.setPageNum(vo.getPageNum());
        dto.setPageSize(vo.getPageSize());
        QueryLoginParamDTO paramDTO = loginLogConverter.toDto(paramVO);
        PageDTO<QueryLoginLogDTO> pageDTO = loginLogService.page(dto, paramDTO);
        List<QueryLoginLogVO> vos = loginLogConverter.toVos(pageDTO.getItems());
        return PageUtil.pageVo(pageDTO, vos);
    }
}
